﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BestBuy.Common;
namespace BestBuy.DAL
{
    public class cManageProduct
    {
        public static int getCountProduct(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.SanPhams.Where(p => p.IDTaiKhoan == idUser).ToList().Count;
        }
        public static int getIdCompany(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == idUser).FirstOrDefault().IDCongTy.Value;
        }
        public static string getNameCompany(int idCompany)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.CongTies.Where(p => p.IDCongTy == idCompany).FirstOrDefault().TenCongTy;
        }
        public static List<SanPham> getListProduct(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.SanPhams.Where(p => p.IDTaiKhoan == idUser && (p.TinhTrang == (int)SanPhamStatus.Unverified || p.TinhTrang==(int)SanPhamStatus.Active)).ToList();
        }
        public static SanPham getProduct(int idProduct)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.SanPhams.Where(p => p.IDSanPham == idProduct).FirstOrDefault();
        }
        public static String getCategoryName(int idCategory)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMuc == idCategory).FirstOrDefault().TenDanhMuc;
        }
        public static void updateProduct(SanPham sp)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.SanPhams.Where(p => p.IDSanPham == sp.IDSanPham).FirstOrDefault();
            kq.ChiTietSanPham = sp.ChiTietSanPham;
            kq.TenSanPham = sp.TenSanPham;
            kq.MaLoaiSanPham = sp.MaLoaiSanPham;
            kq.TuKhoa = sp.TuKhoa;
            kq.MoTa = sp.MoTa;
            kq.HinhAnh = sp.HinhAnh;
            kq.TaiLieu = sp.TaiLieu;
            kq.KyHanGiaoHang = sp.KyHanGiaoHang;
            kq.KhaNangCungCap = sp.KhaNangCungCap;
            kq.NoiXuatXu = sp.NoiXuatXu;
            kq.SoLuongDatToiThieu = sp.SoLuongDatToiThieu;
            kq.PhuongThucThanhToan = sp.PhuongThucThanhToan;
           // kq.IDTaiKhoan = sp.IDTaiKhoan;
            //kq.TinhTrang = sp.TinhTrang;
            context.SubmitChanges();
        }
        public static String getImageName(int idsp)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.SanPhams.Where(p => p.IDSanPham == idsp).FirstOrDefault().HinhAnh;
        }

        public static void deleteProduct(int idsp)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.SanPhams.Where(p => p.IDSanPham == idsp).FirstOrDefault();
            if (kq != null)
            {
                kq.TinhTrang = (int)SanPhamStatus.Deleted;
                context.SubmitChanges();
            }
        }
        public static List<SanPham> searchProduct(int iduser, String name)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.SanPhams.Where(p => p.IDTaiKhoan == iduser && p.TenSanPham.CompareTo(name) == 0).ToList();
            return kq;
        }
    }
}